// pages/personal/myCollection/myCollection.js
var utils = require('../../../utils/util.js');
var httpUrl = getApp().globalData.httpUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentPage:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.type == 1) {
            wx.setNavigationBarTitle({
                title: '我的收藏',
            })
        }
        if (options.type == 2) {
            wx.setNavigationBarTitle({
                title: '观看历史',
            })
        }
        utils.showLoading();
        this.oNslideList(options.type, this.data.currentPage);
        this.setData({
            type:options.type
        })
    },
    //我的收藏  类型（默认为1）1-收藏，2-观看历史 
    oNslideList: function (type) {
        var that = this;
       
        utils.http(httpUrl + "/mobile/member/my_col_or_watch_history", { type: type, page:that.data.currentPage}, function (res) {
            utils.hideLoading();
            if (res.state == 1) {
                // console.log(res);
                var listpost = that.data.list;
                if (that.data.list == undefined) {
                    listpost = [];
                }
                for(var i in res.datas){
                    var post=res.datas[i];
                    listpost.push(post);
                }

                if (res.datas.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '加载更多',
                    });
                }
                that.setData({
                    list: listpost,
                    ready: res.datas.length >= 0 ? 1 : 2,
                });
            } else {
                utils.message(res.datas);
            }
        })
    },

    //点击播放按钮，封面图片隐藏,播放视频
    bindplay: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.id;
        var itemid = e.currentTarget.dataset.itemid;
        //停止正在播放的视频
        var videoContextPrev = wx.createVideoContext("myVideo" + that.data._index)
        videoContextPrev.seek(0)
        videoContextPrev.pause();
        // setTimeout(function () {
        //     //将点击视频进行播放
        //     var videoContext = wx.createVideoContext('myVideo' + index)
        //     videoContext.play();
        //     that.data._index = index;
        // }, 100)
        utils.showLoading()
        utils.http(httpUrl + "/mobile/article/video_article_detail", {
            id: itemid
        }, function (res) {
            utils.hideLoading();
            if (res.state == 1) {
                that.setData({
                    videoDetail: res.datas
                })
                that.setData({
                    id: index
                })
                var videoContext = wx.createVideoContext('myVideo' + index)
                videoContext.play();
                that.data._index = index;

            } else {
                wx.showModal({
                    title: '提示',
                    content: res.datas,
                    success(res) {
                        if (res.confirm) {
                            utils.http(httpUrl + "/mobile/article/add_order", {
                                id: itemid
                            }, function (res) {
                                wx.requestPayment({
                                    timeStamp: res.timeStamp,
                                    nonceStr: res.nonceStr,
                                    package: res.package,
                                    signType: res.signType,
                                    paySign: res.paySign,
                                    success(res) {
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: 'success',
                                            duration: 2000
                                        })
                                        that.setData({
                                            id: index
                                        })
                                        var videoContext = wx.createVideoContext('myVideo' + index)
                                        videoContext.play();
                                        that.data._index = index
                                    },
                                    fail(res) {
                                        that.setData({
                                            id: -0
                                        })
                                    }
                                })
                            })

                        } else if (res.cancel) {
                            that.setData({
                                id: -0
                            })
                        }
                    }
                })

            }

        })

    },

    //上拉加载更多
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 1) {
            var currentPage = that.data.currentPage;
            currentPage++;
            that.setData({
                currentPage: currentPage
            })
            that.oNslideList(that.data.type,that.data.currentPage);
        }

     


    },
    //进入新闻详情
    noNewmoreDetailTap: function (e) {
        wx.navigateTo({
            url: '../../newmore/newmoreDetail/newmoreDetail?id=' + e.currentTarget.dataset.id + "&type=" + e.currentTarget.dataset.type,
        })
    },
})